package com.zhiying.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.zhiying.config.StatusEnum;
import com.zhiying.domain.CMS_Organization;
import com.zhiying.domain.CMS_User;
import com.zhiying.mybatis.OrganizationMapper;
import com.zhiying.page.BasePageDTO;
import com.zhiying.page.PageInfo;
import com.zhiying.service.IOrganizationService;

@Service
@Component
@Transactional
public class IOrganizationServiceImpl implements IOrganizationService {

	@Autowired
	private OrganizationMapper organizationMapper;

	@Override
	public BasePageDTO<CMS_Organization> selectCMSOrganizationAll(String search, PageInfo pageInfo) {
		Page<CMS_Organization> page=new Page<>(pageInfo.getCurrent(),pageInfo.getLimit());
		List<CMS_Organization> list=organizationMapper.selectCMSOrganizationAll(search, pageInfo, page);
		pageInfo.setTotal(count(search));
		return new BasePageDTO<CMS_Organization>(pageInfo,list);
	}

	@Override
	public int count(String search) {
		// TODO Auto-generated method stub
		return organizationMapper.count(search);
	}

	@Override
	public int updateStatus(int organizationId) {
		CMS_Organization cmsorganization = seelctOrganizationById(organizationId);
		if (cmsorganization != null && StatusEnum.NORMAL.getStatus().equals(cmsorganization.getStatus())) {
			CMS_Organization updateUser = new CMS_Organization();
			updateUser.setOrganizationId(Long.valueOf(organizationId));
			updateUser.setStatus(0);
			return organizationMapper.updateStatus(updateUser);
		} else if (cmsorganization != null && StatusEnum.FREEZE.getStatus().equals(cmsorganization.getStatus())) {
			CMS_Organization updateUser = new CMS_Organization();
			updateUser.setOrganizationId(Long.valueOf(organizationId));
			updateUser.setStatus(1);
			return organizationMapper.updateStatus(updateUser);
		}
		return 0;
	}

	@Override
	public CMS_Organization seelctOrganizationById(int organizationId) {
		// TODO Auto-generated method stub
		return organizationMapper.seelctOrganizationById(organizationId);
	}

	@Override
	public int selectDeleteCount(int organizationId) {
		// TODO Auto-generated method stub
		return organizationMapper.selectDeleteCount(organizationId);
	}

	@Override
	public int deleteOrg(int organizationId) {
		// TODO Auto-generated method stub
		int count =selectDeleteCount(organizationId);
		if(count>0) {
		    return count;
		}else if(count==0){
			return organizationMapper.deleteOrg(organizationId);
		}
		return 0;
	}

	@Override
	public BasePageDTO<CMS_User> selectOrgInfo(String search, PageInfo pageInfo, int organizationId) {
		Page<CMS_User> page=new Page<>(pageInfo.getCurrent(),pageInfo.getLimit());
		List<CMS_User> list=organizationMapper.selectOrgInfo(search, pageInfo, page, organizationId);
		pageInfo.setTotal(selectOrgCounts(organizationId, search));
		return new BasePageDTO<CMS_User>(pageInfo,list);
	}

	@Override
	public int selectOrgCounts(int organizationId, String search) {
		// TODO Auto-generated method stub
		return organizationMapper.selectOrgCounts(organizationId, search);
	}
	

}
